//
//  TCZStatusCell.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/25.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
import SnapKit
let margin:CGFloat = 8

class TCZStatusCell: UITableViewCell {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置cell的背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        //添加自定义控件
        prepareUI()
    }
    //记录cell的bottomView的顶部约束
    var bottomViewTopConstraint:Constraint?
    
    //statuses模型属性
    var status:CZStatuses?{
        //赋值后进行更新ui
        didSet{
        //给原创模型赋值
          originalView.status = status
            //卸载原有的约束
          self.bottomViewTopConstraint?.uninstall()
        //转发模型
            if let statusReweet = status?.retweeted_status{
                
                retweetView.hidden = false
                retweetView.retweetStatus = statusReweet
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
            }else{
                
                 retweetView.hidden = true
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                    
                })
                
            }
            
        }
        
    }
    
    //添加自定义控件，并添加约束
    func prepareUI(){
        //添加控件
        contentView.addSubview(originalView)
        //添加转发视图
        contentView.addSubview(retweetView)
        //添加底部视图
        contentView.addSubview(bottomView)
        //添加约束
        //首先是添加cell中contentView的相对约束
        contentView.snp_makeConstraints { (make) -> Void in
        
            make.leading.trailing.top.equalTo(self)
            //底部应该与底部视图一致
            make.bottom.equalTo(bottomView)
            
        }
        //添加originalView的约束
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView).offset(margin)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            //设置随机高度用于测试
           // make.height.equalTo(80+Int(arc4random_uniform(60)))
            
        }
        //retweetView的布局
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(contentView)
        }
        
        //添加bottomView的约束
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.trailing.leading.equalTo(contentView)
          self.bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(44)
            
        }
        //异步绘制，可以避免在主线程上进行绘制，从未优化性能
        self.layer.drawsAsynchronously = true
        //格栅化/光栅化，在没有动画是可以使用，会导致离屏渲染,下面的两个参数必须同时设置才贵有效果
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.mainScreen().scale
        
    }
    
    //懒加载原发模块视图
    private lazy var originalView:CZOriginalView = CZOriginalView()
    //转载视图
    private lazy var retweetView:CZRetweetView = CZRetweetView()
    //懒加载底部按钮视图
    private lazy var bottomView:CZBottomView = CZBottomView()
    
}
